package com.example.cslp.web;



import com.example.cslp.exception.handler.ServiceException;
import lombok.Data;

/**
 * 用于封装服务器端向客户端响应结果的类型
 */
@Data
public final class JsonResult {

    /**
     * 业务状态码
     */
    private Integer code;
    /**
     * 错误时的消息
     */
    private String message;
    /**
     * 处理成功时，需要响应到客户端的数据
     */
    private Object data;

    public static JsonResult ok(String message) {
        return ok(ServiceCode.OK,message);//不太了解ok().getMessage()是什么，可能会出问题
    }

    public static JsonResult ok(Object data,String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = ServiceCode.OK;
        jsonResult.data = data;
        jsonResult.message=message;
        return jsonResult;
    }

    public static JsonResult ok(Integer code, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = code;
        jsonResult.message=message;
        return jsonResult;
    }


    public static JsonResult fail(ServiceException e) {
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult fail(Integer code, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = code;
        jsonResult.message = message;
        return jsonResult;
    }

}
